package com.eye.catcher.controller;


import com.eye.catcher.constants.SbCode;
import com.eye.catcher.param.CommonResponse;
import com.eye.catcher.param.ResponseData;
import com.eye.catcher.param.ResponseUtil;
import com.eye.catcher.rabbit.MQDto;
import com.eye.catcher.service.generate.ITSsDaStatisticsInfoService;
import com.eye.catcher.system.yw.Info.param.BaseInfoDto;
import com.eye.catcher.system.yw.Info.param.BaseInfoPage;
import com.eye.catcher.system.yw.statistics.param.ObserveHasCreateInfoDto;
import com.eye.catcher.system.yw.statistics.param.ObserveHasCreateInfoPage;
import com.eye.catcher.system.yw.statistics.param.StatisticsInfoDto;
import com.eye.catcher.system.yw.statistics.param.StatisticsInfoPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huzhiwei
 * @since 2022-08-03
 */
@Slf4j
@RestController
@RequestMapping("/eyeCatcher-system/statistics-info")
public class TSsDaStatisticsInfoController extends TSysDaBaseController {

    private final static String MODULE_CODE = "/statistics-info";
    private final static String MODULE_NAME = "统计模块";

    @Autowired
    private ITSsDaStatisticsInfoService statisticsInfoService;


    //常规 观测期统计
    @PostMapping("/getStatisticsPageList")
    @ResponseBody
    public ResponseData getStatisticsPageList(@RequestBody StatisticsInfoDto statisticsInfo) {
        String methodCode = "/getStatisticsPageList";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_QUERY,statisticsInfo.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            statisticsInfo.setUserId(userId);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.statisticsInfoService.getStatisticsPageList(
                            StatisticsInfoPage.builder()
                                    .statisticsInfo(statisticsInfo)
                                    .page(this.getPageInfo(statisticsInfo))
                                    .build()
                    )
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     * 观测期统计 导出
     */
    @RequestMapping(path = "/exportStatistics")
    public @ResponseBody
    ResponseData exportStatistics(HttpServletRequest request, HttpServletResponse response,
                                  @RequestBody StatisticsInfoDto statisticsInfo) {
        String methodCode = "/exportStatistics";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId, MODULE_CODE, MODULE_NAME, getClass(),
                    SbCode.OPERATION_DOWLOAD, null, null, null);
            this.sendInitRabbitMqLog(mqDto);
            statisticsInfo.setUserId(userId);
            String msg = this.statisticsInfoService.exportStatistics(statisticsInfo,request,response);
            if (StringUtils.isNotEmpty(msg)) {
                commonResponse = CommonResponse.getInstance();
                commonResponse.setCode(SbCode.SYSTEM_ERROR.getCode());
                commonResponse.setMsg(msg);
                commonResponse.setData(null);
            } else {
                commonResponse = CommonResponse.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        } finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }


    //城市 面积加权 统计
    @PostMapping("/getPlantingStatisticsPageList")
    @ResponseBody
    public ResponseData getPlantingStatisticsPageList(@RequestBody StatisticsInfoDto statisticsInfo) {
        String methodCode = "/getPlantingStatisticsPageList";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_QUERY,statisticsInfo.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            statisticsInfo.setUserId(userId);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.statisticsInfoService.getPlantingStatisticsPageList(
                            StatisticsInfoPage.builder()
                                    .statisticsInfo(statisticsInfo)
                                    .page(this.getPageInfo(statisticsInfo))
                                    .build()
                    )
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    /**
     * 城市种植面积统计 导出
     */
    @RequestMapping(path = "/exportPlantingStatistics")
    public @ResponseBody
    ResponseData exportPlantingStatistics(HttpServletRequest request, HttpServletResponse response,
                                  @RequestBody StatisticsInfoDto statisticsInfo) {
        String methodCode = "/exportPlantingStatistics";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId, MODULE_CODE, MODULE_NAME, getClass(),
                    SbCode.OPERATION_DOWLOAD, null, null, null);
            this.sendInitRabbitMqLog(mqDto);
            statisticsInfo.setUserId(userId);
            String msg = this.statisticsInfoService.exportPlantingStatistics(statisticsInfo,request,response);
            if (StringUtils.isNotEmpty(msg)) {
                commonResponse = CommonResponse.getInstance();
                commonResponse.setCode(SbCode.SYSTEM_ERROR.getCode());
                commonResponse.setMsg(msg);
                commonResponse.setData(null);
            } else {
                commonResponse = CommonResponse.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        } finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    //城市 面积加权 类型 统计
    @PostMapping("/getPlantingTypeStatisticsPageList")
    @ResponseBody
    public ResponseData getPlantingTypeStatisticsPageList(@RequestBody StatisticsInfoDto statisticsInfo) {
        String methodCode = "/getPlantingTypeStatisticsPageList";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_QUERY,statisticsInfo.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            statisticsInfo.setUserId(userId);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.statisticsInfoService.getPlantingTypeStatisticsPageList(
                            StatisticsInfoPage.builder()
                                    .statisticsInfo(statisticsInfo)
                                    .page(this.getPageInfo(statisticsInfo))
                                    .build()
                    )
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    /**
     * 城市种植面积统计 类型 导出
     */
    @RequestMapping(path = "/exportPlantingTypeStatistics")
    public @ResponseBody
    ResponseData exportPlantingTypeStatistics(HttpServletRequest request, HttpServletResponse response,
                                          @RequestBody StatisticsInfoDto statisticsInfo) {
        String methodCode = "/exportPlantingTypeStatistics";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId, MODULE_CODE, MODULE_NAME, getClass(),
                    SbCode.OPERATION_DOWLOAD, null, null, null);
            this.sendInitRabbitMqLog(mqDto);
            statisticsInfo.setUserId(userId);
            String msg = this.statisticsInfoService.exportPlantingTypeStatistics(statisticsInfo,request,response);
            if (StringUtils.isNotEmpty(msg)) {
                commonResponse = CommonResponse.getInstance();
                commonResponse.setCode(SbCode.SYSTEM_ERROR.getCode());
                commonResponse.setMsg(msg);
                commonResponse.setData(null);
            } else {
                commonResponse = CommonResponse.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        } finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    //地区 统计
    @PostMapping("/getRegionStatisticsPageList")
    @ResponseBody
    public ResponseData getRegionStatisticsPageList(@RequestBody StatisticsInfoDto statisticsInfo) {
        String methodCode = "/getRegionStatisticsPageList";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            statisticsInfo.setUserId(userId);
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_QUERY,statisticsInfo.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.statisticsInfoService.getRegionStatisticsPageList(
                            StatisticsInfoPage.builder()
                                    .statisticsInfo(statisticsInfo)
                                    .page(this.getPageInfo(statisticsInfo))
                                    .build()
                    )
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     * 地区 统计 导出
     */
    @RequestMapping(path = "/exportRegionStatistics")
    public @ResponseBody
    ResponseData exportRegionStatistics(HttpServletRequest request, HttpServletResponse response,
                                          @RequestBody StatisticsInfoDto statisticsInfo) {
        String methodCode = "/exportRegionStatistics";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId, MODULE_CODE, MODULE_NAME, getClass(),
                    SbCode.OPERATION_DOWLOAD, null, null, null);
            this.sendInitRabbitMqLog(mqDto);
            statisticsInfo.setUserId(userId);
            String msg = this.statisticsInfoService.exportRegionStatistics(statisticsInfo,request,response);
            if (StringUtils.isNotEmpty(msg)) {
                commonResponse = CommonResponse.getInstance();
                commonResponse.setCode(SbCode.SYSTEM_ERROR.getCode());
                commonResponse.setMsg(msg);
                commonResponse.setData(null);
            } else {
                commonResponse = CommonResponse.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        } finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }



    //地区 基地类型 统计
    @PostMapping("/getRegionTypeStatisticsPageList")
    @ResponseBody
    public ResponseData getRegionTypeStatisticsPageList(@RequestBody StatisticsInfoDto statisticsInfo) {
        String methodCode = "/getRegionTypeStatisticsPageList";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            statisticsInfo.setUserId(userId);
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_QUERY,statisticsInfo.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.statisticsInfoService.getRegionTypeStatisticsPageList(
                            StatisticsInfoPage.builder()
                                    .statisticsInfo(statisticsInfo)
                                    .page(this.getPageInfo(statisticsInfo))
                                    .build()
                    )
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     * 地区 基地类型 统计 导出
     */
    @RequestMapping(path = "/exportRegionTypeStatistics")
    public @ResponseBody
    ResponseData exportRegionTypeStatistics(HttpServletRequest request, HttpServletResponse response,
                                        @RequestBody StatisticsInfoDto statisticsInfo) {
        String methodCode = "/exportRegionTypeStatistics";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId, MODULE_CODE, MODULE_NAME, getClass(),
                    SbCode.OPERATION_DOWLOAD, null, null, null);
            this.sendInitRabbitMqLog(mqDto);
            statisticsInfo.setUserId(userId);
            String msg = this.statisticsInfoService.exportRegionTypeStatistics(statisticsInfo,request,response);
            if (StringUtils.isNotEmpty(msg)) {
                commonResponse = CommonResponse.getInstance();
                commonResponse.setCode(SbCode.SYSTEM_ERROR.getCode());
                commonResponse.setMsg(msg);
                commonResponse.setData(null);
            } else {
                commonResponse = CommonResponse.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        } finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }



    //基地 观测期 搜否创建 统计
    @PostMapping("/getObserveHasCreatePageList")
    @ResponseBody
    public ResponseData getObserveHasCreatePageList(@RequestBody ObserveHasCreateInfoDto observeHasCreateInfo) {
        String methodCode = "/getObserveHasCreatePageList";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            observeHasCreateInfo.setUserId(userId);
            mqDto = new MQDto(methodCode,userId,MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_QUERY,observeHasCreateInfo.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.statisticsInfoService.getObserveHasCreatePageList(
                            ObserveHasCreateInfoPage.builder()
                                    .observeHasCreateInfo(observeHasCreateInfo)
                                    .page(this.getPageInfo(observeHasCreateInfo))
                                    .build()
                    )
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error(e);
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     * 基地 观测期 是否创建 导出
     */
    @RequestMapping(path = "/exportObserveHasCreateStatistics")
    public @ResponseBody
    ResponseData exportObserveHasCreateStatistics(HttpServletRequest request, HttpServletResponse response,
                                            @RequestBody ObserveHasCreateInfoDto observeHasCreateInfo) {
        String methodCode = "/exportObserveHasCreateStatistics";
        MQDto mqDto = null;
        try{
            String userId = getLoginUserId();
            mqDto = new MQDto(methodCode,userId, MODULE_CODE, MODULE_NAME, getClass(),
                    SbCode.OPERATION_DOWLOAD, null, null, null);
            this.sendInitRabbitMqLog(mqDto);
            observeHasCreateInfo.setUserId(userId);
            String msg = this.statisticsInfoService.exportObserveHasCreateStatistics(observeHasCreateInfo,request,response);
            if (StringUtils.isNotEmpty(msg)) {
                commonResponse = CommonResponse.getInstance();
                commonResponse.setCode(SbCode.SYSTEM_ERROR.getCode());
                commonResponse.setMsg(msg);
                commonResponse.setData(null);
            } else {
                commonResponse = CommonResponse.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        } finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
}
